package com.pms.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pms.util.DateUtil;
import com.pms.util.UU;
import com.pms.dao.IdeaDao;
import com.pms.dao.IdeacommentDao;
import com.pms.dao.UserDao;
import com.pms.entity.Idea;
import com.pms.entity.Ideacomment;
import com.pms.entity.User;

public class IdeaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IdeaDao dao=new IdeaDao();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  String m=request.getParameter("_m");
		  if("save".equals(m)){
			  save(request,response);
		  }else if("comment".equals(m)){
			  comment(request,response);
		  }else if("look".equals(m)){
			  look(request,response);
		  }else if("del".equals(m)){
			  del(request,response);
		  }else if("edit".equals(m)){
			  edit_idea(request,response);
		  }else if("es".equals(m)){
			  edit_save(request,response);
		  }else if("new".equals(m)){
			  new_idea(request,response);
		  }else if("".equals(m)||m==null){
			  findAll(request,response);
		  }
	}
	private void comment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = UU.getPk();
		String content = request.getParameter("comment");
		String createtime =DateUtil.getNow();
		String userid = request.getParameter("userid");
		String ideaid = request.getParameter("ideaid");
		
		UserDao userdao = new UserDao();
		User user = userdao.findById(userid);
		IdeacommentDao ideacommentdao = new IdeacommentDao();
		ideacommentdao.save(id, content, ideaid, userid, createtime);
		List<Ideacomment> ideacomment = ideacommentdao.findById(userid);
		request.setAttribute("user", user);
		request.setAttribute("ideacomment", ideacomment);
		request.getRequestDispatcher("/WEB-INF/views/shareidea.jsp").forward(request, response);		
	}
	private void look(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		HttpSession session = request.getSession();
		Idea idea = dao.findById(id);
		
		session.setAttribute("idea", idea);
		request.getRequestDispatcher("/WEB-INF/views/shareidea.jsp").forward(request, response);		
	}
	
	private void edit_save(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
			String id=request.getParameter("id");
			String ideaname=request.getParameter("ideaname");
			String text=request.getParameter("text");
			String createtime=DateUtil.getNow();
			Idea idea=new Idea();
			idea.setId(id);
			idea.setIdeaname(ideaname);
			idea.setText(text);
			idea.setCreatetime(createtime);
			dao.udate(idea);
			response.sendRedirect("idea.jspx");

	
	}
	private void edit_idea(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException  {
		 	String id=request.getParameter("id");
		 	Idea idea=dao.findById(id);
		 	request.setAttribute("idea", idea);
			request.getRequestDispatcher("/WEB-INF/views/edit_idea.jsp").forward(request, response);

	}
	private void new_idea(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException  {
		request.getRequestDispatcher("/WEB-INF/views/new_idea.jsp").forward(request, response);

	}
	private void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			String id=request.getParameter("id");
			dao.del(id);
			response.sendRedirect("idea.jspx");

	}
	private void findAll(HttpServletRequest request,HttpServletResponse response)
			throws ServletException, IOException {
			List<Idea> idea=dao.findAll();
			request.setAttribute("idea", idea);
			request.getRequestDispatcher("/WEB-INF/views/idea.jsp").forward(request, response);

	}
	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String id=UU.getPk();
		String ideaname=request.getParameter("ideaname");
		String text=request.getParameter("text");
		String t_project_id=request.getParameter("projectid");
		String createtime=DateUtil.getNow();
		
		dao.save(id, ideaname, text, t_project_id, createtime);
		response.sendRedirect("idea.jspx");
		
	}
}
